package com.example.a_java.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.a_java.common.Result;
import com.example.a_java.dto.AccountCreateDTO;
import com.example.a_java.dto.AccountTransactionDTO;
import com.example.a_java.entity.Account;
import com.example.a_java.service.AccountService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 账户管理控制器
 */
@Tag(name = "账户管理", description = "账户管理相关接口")
@RestController
@RequestMapping("/account")
@SecurityRequirement(name = "bearer-key")
public class AccountController {

    @Autowired
    private AccountService accountService;

    /**
     * 创建账户
     */
    @Operation(summary = "创建账户", description = "创建新账户")
    @PostMapping
    @PreAuthorize("hasAuthority('account:create')")
    public Result<Account> createAccount(@RequestBody @Valid AccountCreateDTO accountCreateDTO) {
        Account account = accountService.createAccount(accountCreateDTO);
        return Result.success(account);
    }

    /**
     * 统一查询账户列表
     */
    @Operation(summary = "查询账户列表", description = "统一的账户查询接口，支持多条件查询")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('account:list')")
    public Result<Page<Account>> list(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "账户号") @RequestParam(required = false) String accountNo,
            @Parameter(description = "账户名称") @RequestParam(required = false) String accountName,
            @Parameter(description = "开户行") @RequestParam(required = false) String bankName,
            @Parameter(description = "账户类型(1:个人账户 2:企业账户)") @RequestParam(required = false) Integer accountType,
            @Parameter(description = "账户状态(0:冻结 1:正常)") @RequestParam(required = false) Integer status,
            @Parameter(description = "关联客户ID") @RequestParam(required = false) Long custId,
            @Parameter(description = "排序字段(balance:余额 openTime:开户时间 createTime:创建时间)") @RequestParam(defaultValue = "createTime") String orderBy,
            @Parameter(description = "排序方式(asc:升序 desc:降序)") @RequestParam(defaultValue = "desc") String orderType) {
        
        // 调用Service层进行统一查询
        Page<Account> accounts = accountService.queryAccounts(page, size, accountNo, accountName, bankName,
                                                           accountType, status, custId, orderBy, orderType);
        return Result.success(accounts);
    }

    /**
     * 根据ID获取账户详情
     */
    @Operation(summary = "获取账户详情", description = "根据ID获取账户详情")
    @GetMapping("/{accountId}")
    @PreAuthorize("hasAuthority('account:query')")
    public Result<Account> getInfo(@PathVariable Long accountId) {
        Account account = accountService.getById(accountId);
        return Result.success(account);
    }

    /**
     * 存款
     */
    @Operation(summary = "存款", description = "向账户存款")
    @PostMapping("/deposit")
    @PreAuthorize("hasAuthority('account:deposit')")
    public Result<Account> deposit(@RequestBody @Valid AccountTransactionDTO accountTransactionDTO) {
        Account account = accountService.deposit(accountTransactionDTO);
        return Result.success(account);
    }

    /**
     * 取款
     */
    @Operation(summary = "取款", description = "从账户取款")
    @PostMapping("/withdraw")
    @PreAuthorize("hasAuthority('account:withdraw')")
    public Result<Account> withdraw(@RequestBody @Valid AccountTransactionDTO accountTransactionDTO) {
        Account account = accountService.withdraw(accountTransactionDTO);
        return Result.success(account);
    }

    /**
     * 冻结账户
     */
    @Operation(summary = "冻结账户", description = "冻结指定账户")
    @PostMapping("/freeze/{accountId}")
    @PreAuthorize("hasAuthority('account:freeze')")
    public Result<Boolean> freezeAccount(
            @Parameter(description = "账户ID") @PathVariable Long accountId,
            @Parameter(description = "冻结原因") @RequestParam(required = false) String remark) {
        boolean success = accountService.freezeAccount(accountId, remark);
        return Result.success(success);
    }

    /**
     * 解冻账户
     */
    @Operation(summary = "解冻账户", description = "解冻指定账户")
    @PostMapping("/unfreeze/{accountId}")
    @PreAuthorize("hasAuthority('account:unfreeze')")
    public Result<Boolean> unfreezeAccount(
            @Parameter(description = "账户ID") @PathVariable Long accountId,
            @Parameter(description = "解冻原因") @RequestParam(required = false) String remark) {
        boolean success = accountService.unfreezeAccount(accountId, remark);
        return Result.success(success);
    }

    /**
     * 冻结金额
     */
    @Operation(summary = "冻结金额", description = "冻结账户部分金额")
    @PostMapping("/freezeAmount")
    @PreAuthorize("hasAuthority('account:freezeAmount')")
    public Result<Account> freezeAmount(@RequestBody @Valid AccountTransactionDTO accountTransactionDTO) {
        Account account = accountService.freezeAmount(accountTransactionDTO);
        return Result.success(account);
    }

    /**
     * 解冻金额
     */
    @Operation(summary = "解冻金额", description = "解冻账户部分金额")
    @PostMapping("/unfreezeAmount")
    @PreAuthorize("hasAuthority('account:unfreezeAmount')")
    public Result<Account> unfreezeAmount(@RequestBody @Valid AccountTransactionDTO accountTransactionDTO) {
        Account account = accountService.unfreezeAmount(accountTransactionDTO);
        return Result.success(account);
    }
} 